import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:wallet_utils/base_router_config.dart';
import 'package:wallet_utils/color.dart';
import 'package:wallet_utils/event_bus_utils.dart';
import 'package:wallet_utils/ht_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:wallet_utils/ht_common_widgets.dart';
import 'package:wallet_utils/msg_event_utils.dart';
import 'package:wallet_utils/report_widget/click_widget.dart';
import 'package:wallet_utils/text_style.dart';
import '../../productPop/product_bootom_sheet.dart';
import 'logic.dart';

class ResultPage extends StatelessWidget {
  final logic = Get.put(ResultLogic());
  final state = Get
      .find<ResultLogic>()
      .state;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () {
        EventBusUtils.getInstance().fire(MsgEventBusUtils(MsgEventBusUtils.popStatus, value: false));
        Get.until((route) => (route.settings.name == BaseRouterConfig.main));
        return Future.value(true);
      },
      child: Scaffold(
        appBar: htCommonAppbar(context, title: '申请结果', leadingAction: () {
          EventBusUtils.getInstance().fire(MsgEventBusUtils(MsgEventBusUtils.popStatus, value: false));
          Get.until((route) => (route.settings.name == BaseRouterConfig.main));
        }),
        body: Stack(
          children: [
            Column(
              children: [
                Expanded(
                  flex: 8,
                  child: Container(
                    decoration: const BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage("assets/result_bg.png"),
                        fit: BoxFit.fill,
                      ),
                    ),
                  ),
                ),
                Expanded(
                  flex: 1,
                  child: Container(
                    color: ColorManager.colorF5F8FF,
                  ),
                ),
              ],
            ),
            GetBuilder<ResultLogic>(builder: (logic) {
              return Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Container(
                    margin: EdgeInsets.only(top: 28.w, bottom: 20.w),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Container(
                          child: Image.asset(
                            'images/icon_result.png',
                            width: 34.w,
                            height: 34.w,
                          ),
                          margin: EdgeInsets.only(bottom: 10.w),
                        ),
                        SizedBox(
                          width: 5.w,
                        ),
                        Column(
                          children: [
                            Text(
                              '您的咨询提交成功，订单受理中',
                              style: TextStyleUtils.style000000(
                                fontSize: 16.sp,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            Text(
                              '请保持电话畅通，以免错过审核电话',
                              style: TextStyleUtils.style000000(fontSize: 12.sp),
                            ),
                          ],
                        ),
                      ],
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 15.w),
                    padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 9.w),
                    decoration: BoxDecoration(
                      color: ColorManager.colorE0EDFF,
                      //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                      borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(8.w),
                        topRight: Radius.circular(8.w),
                      ),
                    ),
                    child: Text(
                      '以下产品已提交审核，将会有机构顾问与您取得联系，请您保持手机畅通',
                      style: TextStyleUtils.color5B90DB(fontSize: 13.sp),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 15.w),
                    padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 9.w),
                    decoration: BoxDecoration(
                      color: ColorManager.colorFFFFFF,
                      //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                      borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(8.w),
                        bottomRight: Radius.circular(8.w),
                      ),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          children: [
                            HtImage(
                              icon: true,
                              url: logic.productInfo?.imgUrl ?? '',
                              width: 24.w,
                              height: 24.w,
                              radius: 2,
                              placeholder: 'images/logo.png',
                              errImage: 'images/logo.png',
                            ),
                            SizedBox(
                              width: 8.w,
                            ),
                            Text(
                              logic.productInfo?.name ?? '',
                              style: TextStyleUtils.style000000(
                                fontSize: 16.w,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            Expanded(
                              child: Text(
                                '机构代码：207',
                                style: TextStyleUtils.style999999(
                                  fontSize: 13.w,
                                ),
                                textAlign: TextAlign.right,
                              ),
                            ),
                          ],
                        ),
                        Container(
                          margin: EdgeInsets.symmetric(vertical: 5.w),
                          child: Text(
                            logic.productInfo?.companyName ?? '',
                            style: TextStyleUtils.style333333(
                              fontSize: 14.w,
                            ),
                          ),
                        ),
                        Text(
                          '*此额度需要机构/资方电话复审 (一小时内) 请注意接听电话',
                          style: TextStyleUtils.styleB3B3B3(
                            fontSize: 12.w,
                          ),
                        ),
                      ],
                    ),
                  ),
                  SizedBox(
                    height: 20.w,
                  ),
                  Visibility(
                    visible: logic.productList!.isEmpty,
                    child: Container(
                      margin: EdgeInsets.symmetric(
                        horizontal: 15.w,
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 15.w),
                      decoration: BoxDecoration(
                        color: ColorManager.colorFFFFFF,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(
                          Radius.circular(8.w),
                        ),
                      ),
                      child: Column(
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Container(
                                width: 15.w,
                                height: 1.2.w,
                                color: ColorManager.color065ECB,
                                margin: EdgeInsets.only(top: 8.w),
                              ),
                              Container(
                                  margin: EdgeInsets.only(top: 20.w, bottom: 13.w,left: 10.w,right: 10.w),
                                  child: Text(
                                    '放鑫钱包优势',
                                    style: TextStyleUtils.color065ECB(fontSize: 14.sp, fontWeight: FontWeight.bold),
                                  )),
                              Container(
                                width: 15.w,
                                height: 1.2.w,
                                color: ColorManager.color065ECB,
                                margin: EdgeInsets.only(top: 8.w),
                              ),
                            ],

                          ),
                          GridView.builder(
                            padding: EdgeInsets.zero,
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                              crossAxisCount: 2,
                              mainAxisSpacing: 15.w,
                              crossAxisSpacing: 15.w,
                              childAspectRatio: 2.7,
                            ),
                            itemCount: state.list.length,
                            itemBuilder: (BuildContext context, int index) {
                              return buildInfoItem(index);
                            },
                          ),
                        ],
                      ),
                    ),

                  ),
                  Visibility(
                    visible: logic.productList!.isNotEmpty,
                    child: Container(
                      margin: EdgeInsets.symmetric(
                        horizontal: 15.w,
                      ),
                      padding: EdgeInsets.symmetric(horizontal: 15.w, vertical: 15.w),
                      decoration: BoxDecoration(
                        color: ColorManager.colorFFFFFF,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(
                          Radius.circular(8.w),
                        ),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Text(
                            '热门推荐',
                            style: TextStyleUtils.style000000(fontSize: 18.w, fontWeight: FontWeight.bold),
                          ),
                          SizedBox(
                            height: 15.w,
                          ),
                          GridView.builder(
                            padding: EdgeInsets.zero,
                            shrinkWrap: true,
                            physics: const NeverScrollableScrollPhysics(),
                            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                              crossAxisCount: 2,
                              mainAxisSpacing: 15.w,
                              crossAxisSpacing: 10.w,
                              childAspectRatio: 1,
                            ),
                            itemCount: logic.productList!.length,
                            itemBuilder: (BuildContext context, int index) {
                              return buildItem(index);
                            },
                          ),
                        ],
                      ),
                    ),

                  ),
                  ClickWidget(
                    onTap: () {
                      EventBusUtils.getInstance().fire(MsgEventBusUtils(MsgEventBusUtils.popStatus, value: false));
                      Get.until((route) => (route.settings.name == BaseRouterConfig.main));
                    },
                    child: Container(
                      width: double.infinity,
                      height: 48.w,
                      alignment: Alignment.center,
                      margin: EdgeInsets.symmetric(horizontal: 15.w, vertical: 20.w),
                      decoration: BoxDecoration(
                        color: ColorManager.colorD93C3A,
                        //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                        borderRadius: BorderRadius.all(
                          Radius.circular(25.w),
                        ),
                      ),
                      child: Text(
                        '查看更多产品',
                        style: TextStyleUtils.styleFFFFFF(
                          fontSize: 18.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 15.w),
                    child: Text(
                      '温馨提示：',
                      style: TextStyleUtils.color5B90DB(fontSize: 11.sp),
                    ),
                  ),
                  Container(
                    margin: EdgeInsets.only(
                      left: 15.w,
                      right: 15.w,
                      bottom: ScreenUtil().bottomBarHeight + 14.w,
                    ),
                    child: Text(
                      '放鑫钱包为您提供贷款咨询推荐服务，在使用服务过程中遇到的任何预先收费，'
                          '网络转账、素要银行卡密码、素要手机验证码行为均有诈骗风险，'
                          '请保持警惕避免损失。同时您知愿您已授权${state.name ?? ''}基于向您'
                          '推荐贷款咨询服务之目的，将您的个人信息提供给贷款咨询服务机构，'
                          '请特别关注机构是否按照您的授权使用您的个人信息并向您提供贷款咨询服务。',
                      style: TextStyleUtils.colorC2C2C2(fontSize: 11.sp),
                    ),
                  ),
                ],
              );
            })
          ],
        ),
      ),
    );
  }

  Widget buildItem(int index) {
    return Stack(
      children: [
        Container(
          decoration: BoxDecoration(
            color: ColorManager.colorF4F9FF,
            //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
            borderRadius: BorderRadius.all(
              Radius.circular(8.w),
            ),
          ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  HtImage(
                    icon: true,
                    url: logic.productList![index].logo ?? '',
                    width: 24.w,
                    height: 24.w,
                    radius: 2,
                    placeholder: 'images/logo.png',
                    errImage: 'images/logo.png',
                  ),
                  SizedBox(
                    width: 2.w,
                  ),
                  Text(
                    logic.productList![index].productName ?? '',
                    style: TextStyleUtils.style000000(
                      fontSize: 16.w,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ],
              ),
              Container(
                margin: EdgeInsets.only(top: 10.w, bottom: 5.w),
                child: Text(
                  '最高额度(元)',
                  style: TextStyleUtils.style999999(
                    fontSize: 12.w,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Text(
                logic.productList![index].loanAmount ?? '',
                style: TextStyleUtils.styleFE5300(
                  fontSize: 20.w,
                  fontWeight: FontWeight.bold,
                ),
              ),
              ClickWidget(
                onTap: (){
                  logic.popover(2,index);
                },
                child: Container(
                  height: 25.w,
                  width: 80.w,
                  margin: EdgeInsets.only(top: 10.w),
                  alignment: const Alignment(0, 0),
                  decoration: BoxDecoration(
                      color: ColorManager.colorFFFFFF,
                      //设置四周圆角 角度 这里的角度应该为 父Container height 的一半
                      borderRadius: BorderRadius.all(Radius.circular(25.w)),
                      border: Border.all(width: 1.w, color: ColorManager.colorD93C3A)),
                  child: Text(
                    '立即申请',
                    style: TextStyleUtils.styleFFECE9(fontSize: 12.sp, fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ],
          ),
        ),
        Positioned(
          right: 0.w,
          child: Image.asset(
            'images/product_badge.png',
            width: 45.w,
            height: 45.w,
          ),
        )
      ],
    );
  }

  Container buildInfoItem(int index) {
    return Container(
      alignment: Alignment.center,
      child: Row(
        children: [
          Image.asset(
            state.list[index].image ?? '',
            width: 40.w,
            height: 40.w,
          ),
          SizedBox(
            width: 8.w,
          ),
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                state.list[index].name ?? '',
                style: TextStyleUtils.style000000(fontSize: 15.sp),
              ),
              SizedBox(
                height: 5.w,
              ),
              Text(
                state.list[index].title ?? '',
                style: TextStyleUtils.style999999(fontSize: 12.sp),
              ),
            ],
          )
        ],
      ),
    );
  }

}
